package com.sm.filemanagelibrary.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.chad.library.adapter.base.entity.AbstractExpandableItem;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.sm.filemanagelibrary.adapter.FolderLiseAdapter;
import com.sm.filemanagelibrary.util.FileTypeUtil;
import com.sm.filemanagelibrary.util.StringArraySort;

import java.io.Serializable;

/**
 * created by zhud on 2019/9/26
 */
public class FileEntity extends AbstractExpandableItem implements Parcelable, MultiItemEntity, Comparable<FileEntity> {
    //文件名
    public String name;
    //绝对路径
    public String path;
    //文件类型
    public String type;
    //文件大小
    public long size;
    //是否文件夹
    public boolean isDirectory;
    //最后修改时间
    public long lastModified;
    //所在文件夹
    public String parent;

    public boolean checked;
    public FileEntity() {
    }


    protected FileEntity(Parcel in) {
        name = in.readString();
        path = in.readString();
        type = in.readString();
        size = in.readLong();
        isDirectory = in.readByte() != 0;
        lastModified = in.readLong();
        parent = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(path);
        dest.writeString(type);
        dest.writeLong(size);
        dest.writeByte((byte) (isDirectory ? 1 : 0));
        dest.writeLong(lastModified);
        dest.writeString(parent);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<FileEntity> CREATOR = new Creator<FileEntity>() {
        @Override
        public FileEntity createFromParcel(Parcel in) {
            return new FileEntity(in);
        }

        @Override
        public FileEntity[] newArray(int size) {
            return new FileEntity[size];
        }
    };

    @Override
    public int compareTo(FileEntity o) {
        if (isDirectory && !o.isDirectory) {
            return -1;
        } else if (!isDirectory && o.isDirectory) {
            return 1;
        } else {
            if (StringArraySort.isBiggerThan(name, o.name)) {
                return 1;
            }
            return -1;
        }
    }

    @Override
    public int getLevel() {
        return 1;
    }

    //获取文件类型
    public FileTypeUtil.Type getType() {
        return FileTypeUtil.toGetFileType(type);
    }

    @Override
    public int getItemType() {
        switch (getType()) {
            case Image:
                return FolderLiseAdapter.TYPE_IMAGE;
            case Video:
            case Audio:
                return FolderLiseAdapter.TYPE_VIDEO;
            case Document:
            case Form:
                return FolderLiseAdapter.TYPE_DOC;
            case APP:
                return FolderLiseAdapter.TYPE_APP;
            default:
                return FolderLiseAdapter.TYPE_OTHERS;
        }
    }
}
